package main;

//Nikolas Wolfe
//2/9/2009
//CIS 4930
//Towers of Hanoi

/*
 *  This is the basic algorithm used by a given thread to execute actions
 *  upon the towers
 */

public class TowerWatcher extends Thread
{
	private HanoiTower myTower;
	private TowersOfHanoi toh;
	private String name;
	private int actionCount = 0;	// can take at most two moves before quitting
	private ExecutionStrategy strategy;
	
	public TowerWatcher(HanoiTower tower, TowersOfHanoi toh, 
			String name)
	{
		this.myTower = tower;
		this.toh = toh;
		this.name = name;
	}

	// calls the strategy supplied by the ExecutionStrategy
	public void run()
	{
		strategy.run();
	}
	
	// change the algorithm
	public void setStategy(ExecutionStrategy stategy)
	{
		this.strategy = stategy;
	}
	
	/* checks that thread has accomplished at least one legal move, and if so
	 * we are ready to yield. otherwise, the run() main loop will terminate
	 * when the thread realizes it has no legal moves left and the thread will
	 * begin to wait
	 */
	public void checkToYieldAction()
	{
		if( actionCount >= 1 )
		{
			actionCount = 0;
			myTower.setReadyToYield(true);
		}
		else { myTower.setReadyToYield(false); }
	}
	
	public String toString()
	{
		return this.name;
	}
	
	/*
	 * Methods used by strategy to externalize 
	 * context state
	 */
	public HanoiTower getMyTower()
	{
		return this.myTower;
	}

	public TowersOfHanoi getToh()
	{
		return this.toh;
	}

	public int getActionCount()
	{
		return this.actionCount;
	}
	
	public void setActionCount(int actionCount)
	{
		this.actionCount = actionCount;
	}
	
}
